package com.yygh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yygh.model.hosp.HospitalSet;
import com.yygh.result.Result;
import com.yygh.service.HospitalSetService;
import com.yygh.utils.MD5;
import com.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    //注入service
    @Autowired
    private HospitalSetService hospitalSetService;

    //1.查询所有医院设置信息
    @ApiOperation(value = "查询所有医院设置信息")
    @GetMapping("/findAll")
    public Result findAll(){

        //调用service方法
        List<HospitalSet> list = hospitalSetService.list();

        return Result.ok(list);

    }

    //2.逻辑删除医院设置信息
    @ApiOperation(value = "逻辑删除医院设置信息")
    @DeleteMapping("/{id}")
    public Result removeHospSet(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //3.条件查询带分页
    @ApiOperation(value = "条件查询带分页")
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result findPage(@PathVariable Integer current,
                           @PathVariable Integer limit,
                           @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {

        Page<HospitalSet> page = new Page<>(current, limit);

        LambdaQueryWrapper<HospitalSet> queryWrapper = new LambdaQueryWrapper<>();

        String hosname = hospitalSetQueryVo.getHosname();

        if (!StringUtils.isEmpty(hosname)) {
            queryWrapper.like(HospitalSet::getHosname, hosname);
        }

        queryWrapper.eq(hospitalSetQueryVo.getHoscode()!= null, HospitalSet::getHoscode, hospitalSetQueryVo.getHoscode());

        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, queryWrapper);


        return Result.ok(hospitalSetPage);
    }

    //4.添加医院设置
    @ApiOperation(value = "添加医院设置")
    @PostMapping("/saveHospSet")
    public Result saveHospSet(@RequestBody HospitalSet hospitalSet) {

        hospitalSet.setStatus(1);

        //设置签名密钥
        Random random = new Random();

        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));


        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //5.根据id查询医院设置
    @ApiOperation(value = "根据id查询医院设置")
    @GetMapping("/getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id) {

        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    //6.修改医院设置
    @ApiOperation(value = "修改医院设置")
    @PutMapping("/updateHospSet")
    public Result updateHospSet(@RequestBody HospitalSet hospitalSet) {

        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //7.批量删除
    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {

        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

}
